//
//  FFPayResultManager.m
//  LLWallet
//
//  Created by tt on 2022/7/7.
//

#import "FFPayResultManager.h"

//#import "WXApi.h"
//#import "WXApiObject.h"
//#import <AlipaySDK/AlipaySDK.h>



@implementation FFPayResultManager

static FFPayResultManager *_instance;


+ (id)allocWithZone:(NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (FFPayResultManager *)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        
    });
    return _instance;
}

-(void) ff_handlePrepayResult:(FFPrepayResult*)result {
    if(!result.forwardType) {
        return;
    }
    if([result.forwardType isEqualToString:@"WX_APP"]) {
        [self handleWXPay:result];
    }else if([result.forwardType isEqualToString:@"ALIPAY_APP"]) {
        [self handleAlipay:result];
    }
    
//    [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult * _Nonnull result) {
//
//    }];
}

-(void) handleWXPay:(FFPrepayResult*)result {

//    NSDictionary *dataDict = result.result;
//    PayReq *payReq = [[PayReq alloc] init];
//    payReq.partnerId = dataDict[@"partnerId"];
//    payReq.prepayId = dataDict[@"prepayid"];
//    payReq.package = dataDict[@"package"];
//    payReq.nonceStr = dataDict[@"noncestr"];
//    payReq.timeStamp = (UInt32)[dataDict[@"timestamp"] longLongValue];
//    payReq.sign = dataDict[@"sign"];
//    
//    [WXApi sendReq:payReq completion:^(BOOL success) {
//        NSLog(@"wxpay-success--%d",success?1:0);
//    }];
}

-(void) handleAlipay:(FFPrepayResult*)result {
    NSDictionary *dataDict = result.result;
    NSString *orderStr  = dataDict[@"orderStr"];
//    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:WKApp.shared.config.appSchemaPrefix callback:^(NSDictionary *resultDic) {
//        NSLog(@"支付宝-->%@",resultDic);
//    }];
}

@end

@implementation FFPrepayResult

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFPrepayResult *result = [FFPrepayResult new];
    result.payNo = dictory[@"pay_no"];
    result.forwardType = dictory[@"forward_type"]?:@"";
    result.outTradeNo = dictory[@"out_trade_no"];
    result.result = dictory[@"result"]?:@{};
    return result;
}

@end
